home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / nbapi.zip / NBCLIENT.C < prev    next >
C/C++ Source or Header  |  1991-06-18  |  6KB  |  270 lines

  1. /********************************************/
  2. /*                                          */
  3. /*    Name:NBCLIENT.C                       */
  4. /*                                          */
  5. /*    Purpose: Transmits a File to or       */
  6. /*               from a Netbios Partner        */
  7. /*                                          */
  8. /*    Revision: 01.00.00                    */
  9. /*        Date: 06/10/91                    */
  10. /*                                          */
  11. /********************************************/
  12.  
  13. /* Include Statements */
  14. #include <stdio.h>
  15. #include <conio.h>
  16. #include <alloc.h>
  17. #include <dos.h>
  18. #include <dir.h>
  19. #include <string.h>
  20. #include "nb_api.c"
  21.  
  22. /* Global Variables */
  23. unsigned int length;
  24. int ret,mode;
  25. unsigned char loc_name_num,target_lsn,session_lsn;
  26. char *buffer,*loc_name,*rem_name,temp[20],temp1[20];
  27.  
  28. /* Global Structures */
  29. #define MAX_BUFF    63000
  30. struct NCB ncb;
  31. struct MSG message;
  32.  
  33. /* Function Definitions */
  34. void create_window(int left, int top, int right, int bottom, int color);
  35. void start_session(void);
  36. void menu();
  37. void dos_cmd(void);
  38. void tx_file(void);
  39. void rx_file(void);
  40. void clean_up(void);
  41.  
  42. void main(void)
  43. {
  44.  
  45.     clrscr();
  46.     /* Allocate Memory for Buffer */
  47.     buffer=(char *)farcalloc(MAX_BUFF+10,sizeof(char));
  48.     if(buffer==NULL){
  49.         printf("ERROR: Unable to allocate main buffer!\r\n");
  50.         exit(-1);
  51.         };
  52.     loc_name=(char *)farcalloc(16,sizeof(char));
  53.     rem_name=(char *)farcalloc(16,sizeof(char));
  54.     if(rem_name==NULL){
  55.         printf("ERROR: Unable to allocate name buffers!\r\n");
  56.         exit(-1);
  57.         };
  58.  
  59.     textcolor(WHITE);
  60.     /* create_window(20,6,60,16,BLUE); */
  61.     cprintf("\r\n\r\n         Netbios CLIENT Utility\r\n");
  62.     cprintf("     Dawn Development Corp.   1991\r\n");
  63.     start_session();
  64.     menu();
  65.  
  66.     /******** Clean Up ***********/
  67.     farfree(buffer);
  68.     farfree(loc_name);
  69.     farfree(rem_name);
  70.     clean_up();
  71.  
  72. }
  73.  
  74. void create_window(int left, int top, int right, int bottom, int color)
  75. {
  76.     textbackground(color);
  77.     window(left,top,right,bottom);
  78.     clrscr();
  79.     /* Make Border */
  80.  
  81.     window(left+1,top+1,right-1,bottom-1);
  82. }
  83.  
  84. void start_session(void)
  85. {
  86.     /***************** START SESSION ************************/
  87.     cprintf("\r\nEnter CLIENT Name: ");
  88.     temp[0]=15;
  89.     loc_name=cgets(temp);
  90.     cprintf("\r\nAdding LOCAL Name to Table\r\n");
  91.     ret=check_nb_install();
  92.     if(ret==0){
  93.         cprintf("\r\nERROR: Netbios NOT Installed..\r\n");
  94.         exit(-2);
  95.         };
  96.     /*********** Add Local Name and Call Remote *************/
  97.     ncb=add_name(loc_name);
  98.     if(ncb.NCB_RETCODE!=0){
  99.         cprintf("ERROR: Unable to ADD Local Name!\r\n");
  100.         cprintf("ERROR: %Xh\r\n",ncb.NCB_RETCODE);
  101.         exit(-1);
  102.         };
  103.     loc_name_num=ncb.NCB_NUM;
  104.     cprintf("\r\nEnter HOST Name: ");
  105.     temp1[0]=15;
  106.     rem_name=cgets(temp1);
  107.     cprintf("\r\nCalling HOST: %s\r\n",rem_name);
  108.     ncb=call(rem_name,loc_name,0);
  109.     if(ncb.NCB_RETCODE!=0){
  110.         cprintf("\r\nERROR: Unable to CALL host!\r\n");
  111.         cprintf("ERROR: %Xh\r\n",ncb.NCB_RETCODE);
  112.         clean_up();
  113.         };
  114.     session_lsn=ncb.NCB_LSN;
  115.     cprintf("Session Established....%02Xh\r\n",session_lsn);
  116. }
  117.  
  118. void menu(void)
  119. {
  120.  
  121.     int x;
  122.  
  123. agn:    clrscr();
  124.     printf("(D)OS Command/(F)ile Transfer/(Q)uit: ");
  125.     x=getch();
  126.     x=toupper(x);
  127.     switch(x){
  128.         case 'D':
  129.             dos_cmd();
  130.         break;
  131.         case 'F':
  132.         agn2:    printf("(S)end/(R)eceive: ");
  133.             x=getch();
  134.             x=toupper(x);
  135.             switch(x){
  136.                 case 'S':
  137.                     tx_file();
  138.                 break;
  139.                 case 'R':
  140.                     rx_file();
  141.                 break;
  142.                 default:
  143.                     goto agn2;
  144.                 break;
  145.                 };
  146.         break;
  147.         case 'Q':
  148.         memset(&message,'\0',sizeof(struct MSG));
  149.         strcpy(message.cmd,"QUIT");
  150.         ncb=send((char *)&message,sizeof(struct MSG),session_lsn);
  151.         if(ncb.NCB_RETCODE!=0){
  152.             cprintf("ERROR: Send Failed!\r\n");
  153.             cprintf("ERROR: %02Xh\r\n",ncb.NCB_RETCODE);
  154.             clean_up();
  155.             };
  156.             return;
  157.                 break;
  158.         default:
  159.             goto agn;
  160.                 break;
  161.         };
  162.  
  163.     goto agn;
  164. }
  165.  
  166. void dos_cmd(void)
  167. {
  168.     memset(&message,'\0',sizeof(struct MSG));
  169.     strcpy(message.cmd,"DOSCMD");
  170.     printf("\r\nEnter DOS Command:\r\n");
  171.     gets(message.text);
  172.     printf("COMMAND: %s\r\n",message.cmd);
  173.     printf("TEXT: %s\r\n",message.text);
  174.     ncb=send((char *)&message,sizeof(struct MSG),session_lsn);
  175.     if(ncb.NCB_RETCODE!=0){
  176.         cprintf("ERROR: Send Failed!\r\n");
  177.         cprintf("ERROR: %02Xh\r\n",ncb.NCB_RETCODE);
  178.         clean_up();
  179.         };
  180.     printf("Waiting for Status Code...");
  181.     ncb=receive((char *)&message,sizeof(struct MSG),session_lsn);
  182.     if(ncb.NCB_RETCODE!=0){
  183.         cprintf("ERROR: Receive Failed!\r\n");
  184.         cprintf("ERROR: %02Xh\r\n",ncb.NCB_RETCODE);
  185.         clean_up();
  186.         };
  187.     ret=strcmp(message.cmd,"STATUS");
  188.     if(ret==0){
  189.         printf("%s\r\n",message.text);
  190.         }
  191.     else{
  192.         printf("\r\nUnkown Message Received\r\n");
  193.         };
  194.  
  195. }
  196.  
  197. void tx_file(void)
  198. {
  199.  
  200.  
  201. }
  202.  
  203. void rx_file(void)
  204. {
  205.     /** Client will receive file ****/
  206.  
  207.     FILE *fl;
  208.     int ret;
  209.     char text[80];
  210.     unsigned count;
  211.     unsigned long len;
  212.  
  213.     /** Send request to Host **/
  214.     memset(&message,'\0',sizeof(struct MSG));
  215.     strcpy(message.cmd,"RXFILE");
  216.     printf("\r\nEnter file to receive from Host: ");
  217.     gets(message.text);
  218.     printf("\r\nEnter file to save as on Client: ");
  219.     gets(text);
  220.     ncb=send((char *)&message,sizeof(struct MSG),session_lsn);
  221.     if(ncb.NCB_RETCODE!=0){
  222.         cprintf("ERROR: Send Failed!\r\n");
  223.         cprintf("ERROR: %02Xh\r\n",ncb.NCB_RETCODE);
  224.         clean_up();
  225.         };
  226.     /** Open file **/
  227.     fl=fopen(text,"w+b");
  228.     if(fl==NULL){
  229.         printf("Unable to CREATE file...\r\n");
  230.         clean_up();
  231.         };
  232.     /* Start Receiving File from Host */
  233.     count=0;
  234.     while(ncb.NCB_LENGTH<MAX_BUFF){
  235.         ncb=receive((char *)&buffer,MAX_BUFF,session_lsn);
  236.         if(ncb.NCB_RETCODE!=0){
  237.             cprintf("ERROR: Receive Failed!\r\n");
  238.             cprintf("ERROR: %02Xh\r\n",ncb.NCB_RETCODE);
  239.             clean_up();
  240.             };
  241.         ret=fwrite(buffer,1,ncb.NCB_LENGTH,fl);
  242.         count+=ret;
  243.         printf("\rReceived %7u bytes...",count);
  244.         };
  245.  
  246.     fclose(fl);
  247.  
  248.  
  249.  
  250. }
  251.  
  252. void clean_up(void)
  253. {
  254.     printf("\r\nTerminating Session...\r\n");
  255.     ncb=hang_up(session_lsn);
  256.     if(ncb.NCB_RETCODE!=0){
  257.         cprintf("ERROR: Hang Up Failed!\r\n");
  258.         cprintf("ERROR: %02Xh\r\n",ncb.NCB_RETCODE);
  259.         exit(-1);
  260.         };
  261.     printf("Removing Local Name from Table...\r\n");
  262.     ncb=del_name(loc_name);
  263.     if(ncb.NCB_RETCODE!=0){
  264.         cprintf("ERROR: Delete Name Failed!\r\n");
  265.         cprintf("ERROR: %02Xh\r\n",ncb.NCB_RETCODE);
  266.         exit(-1);
  267.         };
  268.     fcloseall();
  269.     exit(0);
  270. }